package nc.impl.arap;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Arrays;

import nc.bs.dao.BaseDAO;
import nc.impl.pub.ace.AceBasTradiscPubServiceImpl;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.arap.bastradisc.AggBasDiscVO;
import nc.vo.arap.bastradisc.BasDiscBVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;

public class BasTradiscMaintainImpl extends AceBasTradiscPubServiceImpl implements nc.itf.arap.IBasTradiscMaintain {

      @Override
    public void delete(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills) throws BusinessException {
        super.pubdeleteBills(clientFullVOs,originBills);
    }
  
      @Override
    public AggBasDiscVO[] insert(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills) throws BusinessException {
        return super.pubinsertBills(clientFullVOs,originBills);
    }
    
      @Override
    public AggBasDiscVO[] update(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills) throws BusinessException {
        return super.pubupdateBills(clientFullVOs,originBills);    
    }
  

  @Override
  public AggBasDiscVO[] query(IQueryScheme queryScheme)
      throws BusinessException {
      return super.pubquerybills(queryScheme);
  }



  @Override
  public AggBasDiscVO[] save(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills)
      throws BusinessException {
      return super.pubsendapprovebills(clientFullVOs,originBills);
  }

  @Override
  public AggBasDiscVO[] unsave(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills)
      throws BusinessException {
      return super.pubunsendapprovebills(clientFullVOs,originBills);
  }

  @Override
  public AggBasDiscVO[] approve(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills)
      throws BusinessException {
      return super.pubapprovebills(clientFullVOs,originBills);
  }

  @Override
  public AggBasDiscVO[] unapprove(AggBasDiscVO[] clientFullVOs,AggBasDiscVO[] originBills)
      throws BusinessException {	
      return super.pubunapprovebills(clientFullVOs,originBills);
  }

  @Override
  public AggBasDiscVO queryMonthBefore(String pk_org, String transtype,String cperiod) throws BusinessException, ParseException {
	  
	  
	  SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM" ); 
	  
		Date date =sdf.parse(cperiod);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String newcperiod  = getLastYearMonth(calendar);

	 
	String sql = "select pk_basdisc from arap_basdisc where dr=0 and PK_ORG = '"+pk_org+"' and TRANSTYPE = '"+transtype+"' and CPERIOD = '"+newcperiod+"';";
	
	  String id = (String) new BaseDAO().executeQuery(sql, new ColumnProcessor());

	  AggBasDiscVO[] bills = new BillQuery<AggBasDiscVO>(AggBasDiscVO.class).query(new String[] {id});
	  
	  if(bills != null && bills.length >0) {
		    for (AggBasDiscVO aggBasDiscVO : bills) {
		       BasDiscBVO[] bodys = aggBasDiscVO.getBodyVOs();
		       List<BasDiscBVO> list = Arrays.asList(bodys);
		       
		       Collections.sort(list, new Comparator<BasDiscBVO>() {
		    	   @Override
		    	public int compare(BasDiscBVO vo1, BasDiscBVO vo2) {
			    		   Double d1 = Double.parseDouble(vo1.getRowno());
			    		   Double d2 = Double.parseDouble(vo2.getRowno());
			    		   
			    		return (int) (d1 - d2);
			    	}
				});
		       bodys = new BasDiscBVO[list.size()];
		       list.toArray(bodys);
		       aggBasDiscVO.setChildrenVO(bodys);
		       
		       return bills[0];
		   }
		  
		    return bills[0];
		 }
	  
	  return null;
}
  

  
  public String getLastYearMonth(Calendar calendar) {
		Calendar now = calendar;
		int month = now.get(Calendar.MONTH);
		String m;
		if(month == 0) {
			return now.get(Calendar.YEAR)-1 + "-" + 12;
		}
		if (month < 10) {
			m = "0" + month;
		} else {
			m = month + "";
		}
		return now.get(Calendar.YEAR) + "-" + m;
	}
}
